import Vue from 'vue'
import Router from 'vue-router'
import Home from './components/Home'

Vue.use(Router)

const originalPush = Router.prototype.push

Router.prototype.push = function push (location) {
    return originalPush.call(this, location).catch(err => err)
}

export default new Router({
    mode: 'history',
    routes: [
        {
            path: '/',
            name: 'Default',
            redirect: '/home',
            component: Home
        },
        {
            path: '/login',
            name: 'Login',
            component: () => import('./components/com/Login'),
            meta: {
                requireAuth: false
            }
        },
        {
            path: '/register',
            name: 'Register',
            component: () => import('./components/com/Register'),
            meta: {
                requireAuth: false
            }
        },
        {
            path: '/admin',
            name: 'Admin',
            component: () => import('./components/admin/AdminIndex'),
            meta: {
                requireAuth: true
            },
            children: [
                {
                    path: '/admin/dashboard',
                    name: 'Dashboard',
                    component: () => import('./components/admin/dashboard/admin/index'),
                    meta: {
                        requireAuth: true
                    }
                }
            ]
        },
        {
            path: '/home',
            name: 'Home',
            component: Home,
            redirect: '/index',
            children: [
                {
                    path: '/index',
                    name: 'AppIndex',
                    component: () => import('./components/home/AppIndex')
                },
                {
                    path: '/article',
                    name: 'Article',
                    component: () => import('./components/article/Articles')
                },
                {
                    path: '/article/details',
                    name: 'Details',
                    component: () => import('./components/article/Details')
                },
                {
                    path: '/admin/content/editor',
                    name: 'Editor',
                    component: () => import('./components/admin/content/ArticleEditor'),
                    meta: {
                        requireAuth: true
                    }
                },
                {
                    path: '/library',
                    name: 'Library',
                    component: () => import('./components/library/LibraryIndex')
                }
            ],
            meta: {
                requireAuth: true
            }
        },
        {
            path: '*',
            component: () => import('./components/share/Error')
        }
    ]
})

// 用于创建默认路由
export const createRouter = new Router({
    mode: 'history',
    routes: [
        {
            path: '/',
            name: 'Default',
            redirect: '/home',
            component: Home
        },
        {
            // home页面并不需要被访问，只是作为其它组件的父组件
            path: '/home',
            name: 'Home',
            component: Home,
            redirect: '/index',
            children: [
                {
                    path: '/index',
                    name: 'AppIndex',
                    component: () => import('./components/home/AppIndex')
                },
                {
                    path: '/article',
                    name: 'Article',
                    component: () => import('./components/article/Articles')
                },
                {
                    path: '/article/details',
                    name: 'Details',
                    component: () => import('./components/article/Details')
                },
                {
                    path: '/admin/content/editor',
                    name: 'Editor',
                    component: () => import('./components/admin/content/ArticleEditor'),
                    meta: {
                        requireAuth: true
                    }
                },
                {
                    path: '/library',
                    name: 'Library',
                    component: () => import('./components/library/LibraryIndex')
                }
            ]
        },
        {
            path: '/login',
            name: 'Login',
            component: () => import('./components/com/Login')
        },
        {
            path: '/register',
            name: 'Register',
            component: () => import('./components/com/Register')
        },
        {
            path: '/admin',
            name: 'Admin',
            component: () => import('./components/admin/AdminIndex'),
            meta: {
                requireAuth: true
            },
            children: [
                {
                    path: '/admin/dashboard',
                    name: 'Dashboard',
                    component: () => import('./components/admin/dashboard/admin/index'),
                    meta: {
                        requireAuth: true
                    }
                }
            ]
        },
        {
            path: '*',
            component: () => import('./components/share/Error')
        }
    ]
})
